package com.example.amazlinglistview.task;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;
import android.view.View;

import com.example.amazlinglistview.MainActivity;
import com.example.amazlinglistview.domain.DealerInfo;
import com.example.amazlinglistview.domain.Dealers;
import com.example.amazlinglistview.utils.NetUtils;

public class CarSeriesDealersTask extends AsyncTask<String, Object, Boolean> {

	private static final String TAG = "CarSeriesDealersTask";
	private MainActivity mContext;

	public CarSeriesDealersTask(MainActivity context) {
		this.mContext = context;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		if (mContext.mProgressbar.getVisibility() != View.VISIBLE) {
			mContext.mProgressbar.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected Boolean doInBackground(String... params) {
		boolean result = false;
		try {
			result = parseSeriesDealerJson(NetUtils.getJson(params[0]));
		} catch (Exception e) {
			new Exception("获取信息失败 ");
		}
		return result;
	}

	private Boolean parseSeriesDealerJson(String json) {
		if (null != json) {
			if (!"null".equalsIgnoreCase(json)) {
				try {
					Dealers dealers = new Dealers();
					JSONObject jsObj = new JSONObject(json);
					List<DealerInfo> dealerInfos = new ArrayList<DealerInfo>();
					dealers.setMsgId(jsObj.isNull("msgId") ? "" : jsObj
							.getString("msgId"));
					dealers.setDealerNum((Integer) (jsObj.isNull("dealerNum") ? ""
							: jsObj.getInt("dealerNum")));
					for (int i = 0; i < jsObj.getJSONArray("dealers").length(); i++) {
						JSONObject dealersObj = jsObj.getJSONArray("dealers")
								.getJSONObject(i);
						DealerInfo dealerInfoItem = new DealerInfo();
						dealerInfoItem.setId(dealersObj.isNull("id") ? ""
								: dealersObj.getString("id"));
						dealerInfoItem
								.setPriceId(dealersObj.isNull("priceId") ? ""
										: dealersObj.getString("priceId"));
						dealerInfoItem.setName(dealersObj.isNull("name") ? ""
								: dealersObj.getString("name"));
						dealerInfoItem.setFullName(dealersObj
								.isNull("fullname") ? "" : dealersObj
								.getString("fullname"));
						dealerInfoItem.setLongitude(dealersObj
								.isNull("longitude") ? "" : dealersObj
								.getString("longitude"));
						dealerInfoItem.setLatitude(dealersObj
								.isNull("latitude") ? "" : dealersObj
								.getString("latitude"));
						dealerInfoItem.setTitle(dealersObj.isNull("title") ? ""
								: dealersObj.getString("title"));
						dealerInfoItem.setUrl(dealersObj.isNull("url") ? ""
								: dealersObj.getString("url"));
						dealerInfoItem.setType(dealersObj.isNull("type") ? ""
								: dealersObj.getString("type"));
						dealerInfoItem.setPrice(dealersObj.isNull("price") ? ""
								: dealersObj.getString("price"));
						dealerInfoItem
								.setAddress(dealersObj.isNull("address") ? ""
										: dealersObj.getString("address"));
						dealerInfoItem.setTelephone(dealersObj
								.isNull("telephone") ? "" : dealersObj
								.getString("telephone"));
						dealerInfoItem.setPermission((Integer) (dealersObj
								.isNull("premission") ? "" : dealersObj
								.getInt("premission")));

						dealerInfos.add(dealerInfoItem);
					}

					dealers.setDealerInfos(dealerInfos);
					List<Dealers> result = new ArrayList<Dealers>();
					result.add(dealers);

					mContext.mDealers = dealers;
				} catch (Exception e) {
					new Exception("读取JSON出错!");
				}
			} else {
				mContext.mDealers = new Dealers();
			}
			return true;
		} else {
			return false;
		}
	}

	@Override
	protected void onPostExecute(Boolean result) {
		super.onPostExecute(result);
		if (result) {
			mContext.mHandler.sendEmptyMessage(2);
		} else {
			mContext.mHandler.sendEmptyMessage(-2);
		}
	}

}
